﻿using System;
using System.Collections;
using System.Configuration;
using System.Web.Configuration;
using HospitalSystem.Core;
using System.Data.SqlClient;
namespace HospitalSystem
{
    public partial class Groups : System.Web.UI.Page
    {

        private GroupsLog _group;
        protected void Page_Load(object sender, EventArgs e)
        {

            _group = new GroupsLog();
            if (!IsPostBack)
            {
                findstaffID();
                _group.BindData(GridView4);
               // _group.crepsnumber(HostID,StaffID);
        
              
                
               }

        }

        //ส่งค่า psnumber id ไปที่ label
        public void psnumbertolabel()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand CmdSql47 = new SqlCommand("SELECT id FROM [ServiceProvision] WHERE id = (SELECT MAX(id)  FROM [ServiceProvision])", scon);
            scon.Open();

            SqlDataReader reader = CmdSql47.ExecuteReader();
            // DataTable dt = new DataTable();
            //  dt.Load(reader);\
            // = reader;

            if (reader.Read())
            {
                Session["id"] = reader["id"].ToString();

                psnumber.Text = (string)Session["id"];

            }
            else
            {

            }
            scon.Close();
            reader.Close();



        }


        public void crepsnumber()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //SqlCommand command2 = new SqlCommand("INSERT INTO [ServiceProvision] (PSNumber,CaseNo,CID,HostID,StaffID,PSDateTime) VALUES(@PSNumber,@CaseNo,@CID,@HostID,@StaffID,@PSDateTime)", scon);
            SqlCommand command2 = new SqlCommand("INSERT INTO [ServiceProvision] (HostID,StaffID,PSDateTime) VALUES(@HostID,@StaffID,@PSDateTime)", scon);
            scon.Open();
            //  command2.Parameters.AddWithValue("@PSNumber", Session["PSNumber"]);
            //  command2.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            // command2.Parameters.AddWithValue("@CID", Session["CID"]);
            command2.Parameters.AddWithValue("@HostID", HostID.Text);
            command2.Parameters.AddWithValue("@StaffID", StaffID.Text);
            command2.Parameters.AddWithValue("@PSDateTime", DateTime.Now);

            command2.ExecuteNonQuery();
            scon.Close();

            psnumbertolabel();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            crepsnumber();
           
            _group.inserttoServiceProvisionVsPerson(GridView4,SelectStat,psnumber);

            sendsession();
          

            Response.Redirect("~/servicerecord2.aspx");

        }



        protected void sendsession()
        {
            Session["id"] = psnumber.Text;


            



        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView2_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }




        protected void findstaffID()
        {



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT Staff_id FROM [Employee] where Username=@Username", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@Username", Session["idlogin"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {
                StaffID.Text = reader2["Staff_id"].ToString();


            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }


    }
}